package com.zhaojun.cloud.service.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zhaojun.cloud.common.mapper.service.ISeckillAccessService;
import com.zhaojun.cloud.common.mapper.service.ISuccesskilledAccessService;
import com.zhaojun.cloud.common.pojo.Seckill;
import com.zhaojun.cloud.common.pojo.Successkilled;
import com.zhaojun.cloud.common.util.AssertionEx;
import com.zhaojun.cloud.common.util.Detect;
import com.zhaojun.cloud.service.order.service.SeckillService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * wwj
 * 2019/1/2  16:55
 */
@Service
@Transactional
public class SeckillServiceImpl implements SeckillService {

    @Autowired
    private ISeckillAccessService iSeckillAccessService;

    @Autowired
    private ISuccesskilledAccessService iSuccesskilledAccessService;

    private final Logger logger = LoggerFactory.getLogger(SeckillServiceImpl.class);

    /**
     * 超卖 成功下的订单有多条
     */
    @Override
    public void dbSeckill(String seckillId, String userId) {
        Seckill seckill = iSeckillAccessService.getById(seckillId);
        AssertionEx.notNull(seckill, "当前商品不存在！");
        long number = seckill.getNumber();
        AssertionEx.isTrue(Detect.isPositive(number), "当前商品没有库存！");
        //有库存
        Successkilled successkilled = new Successkilled();
        successkilled.setUserId(userId);
        successkilled.setState("0"); //状态标识:-1:无效 0:成功 1:已付款 2:已发货
        iSuccesskilledAccessService.save(successkilled);
        //减一个库存
        iSeckillAccessService.updateSeckillStock(seckill.getId());
    }
}
